from django.contrib import admin
from api.models.user import User, Address, ShipAddress
from api.models.product import Porduct, PorductImage
from api.models.stock_record import StockPrice, StockRecord
from api.models.order import Order, OrderLine, OrderStatusChange
from api.models.basket import Basket, BasketLine
from api.models.payment import Payment, Payrecord


class PayrecordAdmin(admin.ModelAdmin):
    list_display = ["id", "order", "trade_num"]


class PaymentAdmin(admin.ModelAdmin):
    list_display = [
        "id",
        "appid",
        "app_debug",
        "alipay_url",
        "alipay_return_url",
        "app_key",
        "ali_key",
    ]


class OrderAdmin(admin.ModelAdmin):
    list_display = ["id", "order_num", "user", "address", "status", "total_rmb", "total_point"]


class OrderStatusChangeAdmin(admin.ModelAdmin):
    list_display = ["id", "order", "old_status", "new_status", "data_created"]


class OrderLineAdmin(admin.ModelAdmin):
    list_display = ["id", "order", "prodcut", "title", "quantity", "pirce"]


class BasketAdmin(admin.ModelAdmin):
    list_display = ["id", "user", "status", "date_merged", "date_submitted"]


class BasketLineAdmin(admin.ModelAdmin):
    list_display = ["id", "product", "basket", "quantity", "pay_type", "choice"]


class ShipAddressAdmin(admin.ModelAdmin):
    list_display = ["id", "user", "province", "city", "area", "detailed", "default", "deleted"]


class AdderssAdmin(admin.ModelAdmin):
    list_display = ["id", "number", "pca", "parent_number"]


class StockPriceAdmin(admin.ModelAdmin):
    list_display = ["id", "type", "price", "stock_record"]
    list_editable = ["type"]


class StockRecordAdmin(admin.ModelAdmin):
    list_display = ["id", "product", "num_in_stock", "num_allocated"]


class UserAdmin(admin.ModelAdmin):
    list_display = (
        "id",
        "username",
        "password",
        "last_name",
        "first_name",
        "mobile",
        "is_superuser",
        "roles",
    )
    search_fields = (
        "id",
        "username",
        "last_name",
        "first_name",
        "mobile",
    )
    list_filter = ["mobile"]


class ProductAdmin(admin.ModelAdmin):
    list_display = ["id", "product_id", "title", "businss_id", "description", "is_public"]
    readonly_fields = ["created_at", "deleted_at"]
    list_editable = ["is_public"]


class ProductImageAdmin(admin.ModelAdmin):
    list_display = ["id", "purpose", "file", "product", "type"]
    readonly_fields = ["created_at", "deleted_at"]


admin.site.register(Payrecord, PayrecordAdmin)
admin.site.register(Payment, PaymentAdmin)
admin.site.register(Address, AdderssAdmin)
admin.site.register(ShipAddress, ShipAddressAdmin)
admin.site.register(Order, OrderAdmin)
admin.site.register(OrderLine, OrderLineAdmin)
admin.site.register(OrderStatusChange, OrderStatusChangeAdmin)
admin.site.register(Basket, BasketAdmin)
admin.site.register(BasketLine, BasketLineAdmin)
admin.site.register(StockRecord, StockRecordAdmin)
admin.site.register(StockPrice, StockPriceAdmin)
admin.site.register(User, UserAdmin)
admin.site.register(PorductImage, ProductImageAdmin)
admin.site.register(Porduct, ProductAdmin)
"""
from django.apps import apps
from django.contrib import admin

from api.models import SMSTemplate

Profile = apps.get_model("api", "Profile")
User = apps.get_model("api", "User")
PanicBuying = apps.get_model("api", "PanicBuying")
PanicBuyingProduct = apps.get_model("api", "PanicBuyingProduct")
PanicBuyingProductPrice = apps.get_model("api", "PanicBuyingProductPrice")
PanicBuyingSMSHistory = apps.get_model("api", "PanicBuyingSMSHistory")
Quota = apps.get_model("api", "Quota")
QuotaRule = apps.get_model("api", "QuotaRule")
QuotaStored = apps.get_model("api", "QuotaStored")
PanicBuyingProductReport = apps.get_model("api", "PanicBuyingProductReport")


def copy_object(modelAdmin, request, queryset):
    for router in queryset:
        pk = router.id
        router.id = None
        router.save()


copy_object.short_description = "复制对象"


class ProfileInline(admin.StackedInline):
    model = Profile
    verbose_name = "配置"
    verbose_name_plural = "配置"


class UserAdmin(admin.ModelAdmin):
    list_display = ("id", "username", "last_name", "first_name", "profile_phone", "date_joined")
    search_fields = (
        "id",
        "username",
        "last_name",
        "first_name",
        "profile__phone",
    )
    inlines = [
        ProfileInline,
    ]
    list_filter = ["profile__phone"]

    def profile_phone(self, obj):
        if obj.profile:
            return "{}".format(obj.profile.phone)
        return "--"

    profile_phone.short_description = "手机号"


class QuotaAdmin(admin.ModelAdmin):
    list_display = (
        "id",
        "manufacture",
        "region",
        "agent_type",
        "grade",
        "panic_buying",
        "product",
        "panic_buying_product",
        "num_agents",
        "quantity",
        "date_created",
    )
    date_hierarchy = "date_created"
    list_filter = (
        "product",
        "panic_buying",
        "panic_buying_product",
        "region",
        "agent_type",
        "grade",
    )
    raw_id_fields = ("panic_buying",)
    list_editable = ("agent_type", "grade", "region", "quantity")
    actions = [copy_object]


class QuotaRuleAdmin(admin.ModelAdmin):
    list_display = (
        "id",
        "name",
        "manufacture",
        "is_default",
        "date_created",
    )
    date_hierarchy = "date_created"
    list_filter = ("is_default", "manufacture")
    list_editable = ("is_default",)
    search_fields = ("name",)


class QuotaStoredAdmin(admin.ModelAdmin):
    list_display = (
        "rule",
        "id",
        "region",
        "agent_type",
        "grade",
        "manufacture",
        "percent",
        "date_created",
    )
    date_hierarchy = "date_created"


class PanicBuyingAdmin(admin.ModelAdmin):
    list_display = (
        "id",
        "name",
        "manufacture",
        "is_public",
        "num_orders",
        "priority",
        "status",
        "start_datetime",
        "end_datetime",
        "audit_end_datetime",
        "date_created",
    )
    list_filter = ("manufacture", "is_public", "status", "priority")
    search_fields = ("name",)
    date_hierarchy = "date_created"
    list_editable = ["is_public", "status", "priority"]


class PanicBuyingProductAdmin(admin.ModelAdmin):
    list_display = ("id", "product", "count", "panic_buying", "date_created")
    list_filter = ("product", "panic_buying")
    date_hierarchy = "date_created"
    raw_id_fields = ("panic_buying",)


class PanicBuyingProductPriceAdmin(admin.ModelAdmin):
    list_display = (
        "id",
        "panic_buying",
        "product",
        "panic_buying_product",
        "agent_type",
        "price",
        "official_price",
        "retail_price",
        "wholesale_price",
        "date_created",
    )
    list_filter = ("product", "panic_buying", "agent_type", "panic_buying_product")
    date_hierarchy = "date_created"
    raw_id_fields = ("panic_buying", "panic_buying_product")
    actions = [copy_object]


class PanicBuyingSMSHistoryAdmin(admin.ModelAdmin):
    list_display = (
        "id",
        "panic_buying",
        "type",
        "phones",
        "operator",
        "created_at",
    )
    list_filter = ["panic_buying", "type", "operator"]
    date_hierarchy = "created_at"
    raw_id_fields = ["panic_buying", "operator"]


class PanicBuyingProductReportAdmin(admin.ModelAdmin):
    list_display = (
        "id",
        "panic_buying_product",
        "file",
        "date_created",
    )
    list_filter = ["panic_buying_product"]
    date_hierarchy = "date_created"


class SMSTemplateAdmin(admin.ModelAdmin):
    list_display = (
        "id",
        "content",
        "type",
        "active",
        "created_at",
    )
    list_filter = ["type", "active"]
    list_editable = ["type", "active"]
    date_hierarchy = "created_at"
    actions = [copy_object]


admin.site.register(User, UserAdmin)
admin.site.register(Quota, QuotaAdmin)
admin.site.register(QuotaRule, QuotaRuleAdmin)
admin.site.register(QuotaStored, QuotaStoredAdmin)
admin.site.register(PanicBuying, PanicBuyingAdmin)
admin.site.register(PanicBuyingProduct, PanicBuyingProductAdmin)
admin.site.register(PanicBuyingProductPrice, PanicBuyingProductPriceAdmin)
admin.site.register(PanicBuyingSMSHistory, PanicBuyingSMSHistoryAdmin)
admin.site.register(PanicBuyingProductReport, PanicBuyingProductReportAdmin)

"""
